package org.tekmob.remindpal.controller;

import java.io.File;
import java.io.IOException;

import org.tekmob.remindpal.R;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class AudioRecordingActivity extends Activity {
	private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";

	private MediaRecorder recorder = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_record_audio2);
		
		setButtonHandlers();
		enableButtons(false);
	}

	private void setButtonHandlers() {
		((ImageButton) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
		((ImageButton) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
	}

	private void enableButton(int id, boolean isEnable) {
		((ImageButton) findViewById(id)).setEnabled(isEnable);
	}

	private void enableButtons(boolean isRecording) {
		enableButton(R.id.btnStart, !isRecording);
		enableButton(R.id.btnStop, isRecording);
	}


	private String getFilename() {
		String filepath = Environment.getExternalStorageDirectory().getPath();
		File file = new File(filepath, AUDIO_RECORDER_FOLDER);

		if (!file.exists()) {
			file.mkdirs();
		}

		return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
	}

	private void startRecording() {
		recorder = new MediaRecorder();

		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		recorder.setOutputFile(getFilename());

		recorder.setOnErrorListener(errorListener);
		recorder.setOnInfoListener(infoListener);

		try {
			recorder.prepare();
			recorder.start();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void stopRecording() {
		if (null != recorder) {
			recorder.stop();
			recorder.reset();
			recorder.release();

			recorder = null;
		}
		/*new AlertDialog.Builder(this).setMessage("Do you want to use this?") 
		.setTitle("Setting")
		.setPositiveButton("OK",  
				new DialogInterface.OnClickListener() {  
			@Override
			public void onClick(DialogInterface dialog, int whichButton){
				String file = AudioRecordingActivity.this.getFilename();
				Intent i = new Intent(AudioRecordingActivity.this, NewReminder.class);
				i.putExtra("FILE", file);
				startActivity(i);
			}  
		})    
		.setNegativeButton("Cancel",  
				new DialogInterface.OnClickListener() {  
			@Override
			public void onClick(DialogInterface dialog, int whichButton){
				dialog.cancel();

			}  
		})  
		.show(); */

	}


	private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
		@Override
		public void onError(MediaRecorder mr, int what, int extra) {
			Toast.makeText(AudioRecordingActivity.this,
					"Error: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
		}
	};

	private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
		@Override
		public void onInfo(MediaRecorder mr, int what, int extra) {
			Toast.makeText(AudioRecordingActivity.this,
					"Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
					.show();
		}
	};

	private View.OnClickListener btnClick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnStart: {
				Toast.makeText(AudioRecordingActivity.this, "Start Recording",
						Toast.LENGTH_SHORT).show();

				enableButtons(true);
				startRecording();

				break;
			}
			case R.id.btnStop: {
				Toast.makeText(AudioRecordingActivity.this, "Stop Recording",
						Toast.LENGTH_SHORT).show();
				enableButtons(false);
				stopRecording();

				break;
			}
			}
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list_reminder, menu);
		return true;
	   
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item){
	    switch(item.getItemId()) {
		    case R.id.new_reminder:
		        //click on create item
		    	Intent newReminder = new Intent(getApplicationContext(),NewReminder.class);
				startActivity(newReminder);
		        break; 
		    case R.id.show:
		        //click on show item
		    	Intent showReminder = new Intent(getApplicationContext(),ShowReminder.class);
				startActivity(showReminder);  	
		        break;
		    case R.id.send:
		        //click on send item
		    	Intent sendReminder = new Intent(getApplicationContext(),SendReminder.class);
				startActivity(sendReminder);      
		        break;
	    }
	    return true;	    
	}
}
